home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15150 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  83 lines

  1. Newsgroups: comp.lang.c
  2. Path: netcom.com!smryan
  3. From: smryan@netcom.com (@#$%!?!)
  4. Subject: Re: Return type - array of structures - please help!
  5. Message-ID: <smryanDpzrwu.95G@netcom.com>
  6. Organization: The Programmer formerly known as S M Ryan
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <4l0c9g$tsr@thorn.cc.usm.edu>
  9. Date: Wed, 17 Apr 1996 05:43:42 GMT
  10. Sender: smryan@netcom8.netcom.com
  11.  
  12. : I wish to make the function called delete to return an array of 
  13. : structures or if that is not possible at least create it so when
  14. : I pass an array of structures, it is by reference and not value like 
  15. : listed below.
  16.  
  17. (I don't know the delicate nuance of C++, so assuming this is for C,)
  18.  
  19. You can't do that. The theory is the compiler must know exactly how much
  20. space to allocate for the return value before enterring the functions,
  21. thus avoiding the need to slide the value on the stack or on a secondary
  22. stack or whatever. Even though standard C only allows constant array
  23. bounds, it still doesn't know how much space to allocate. Go figure.
  24.  
  25. (1) If you are always go to use fixed size arrays, wrap them in a 
  26. struct:
  27.  
  28.     enum {numberStudents=...};
  29.     typedef struct{student s[numberStudents];} Students;
  30.  
  31.     Students delete_1(char*,class*) {
  32.         Students temp;
  33.         ...temp.s[i] = ....
  34.         return temp;
  35.     }
  36.  
  37. (2) Use pointers,
  38.  
  39.     typedef student *Students;
  40.  
  41.     Students delete_2a(char*,class*) {
  42.         static temp[numberStudents];
  43.         ...temp[i] =...
  44.         return temp;
  45.     }
  46. or
  47.     Students delete_2b(char*,class*) {
  48.         Students temp = malloc(sizeof(student)*numberStudents);
  49.         ...temp[i] =...
  50.         return temp;
  51.     }
  52. but _never_
  53.     Students delete_2c(char*,class*) {
  54.         Students temp[numberStudents];
  55.         ...temp[i] =...
  56.         return temp;
  57.     }
  58.  
  59. In delete_1, the array size has a fixed maximum, but it can be passed
  60. and returned freely. Some C compilers might do strange things when returning
  61. structs, so check yours out. You can also assign the whole array with
  62. '=':
  63.     Students class1 = class2;
  64.  
  65. In delete_2a, the array size is again fixed, and every call to delete_2a
  66. will overwrite the results of a previous call. In delete_2b, the array size
  67. can be determined dynamically, and each call produces an independent result.
  68. The caller must take responsibility for releasing the storage by calling
  69. free on a result which is no longer needed. You must use element by element
  70. assignments or memcpy to assign the whole array.
  71.  
  72.     student class1[25];
  73.     memcpy(class1,class2,n*sizeof(student));
  74.  
  75. delete_2c can produce random results. The auto storage for temp in 2c is
  76. released when the function exits so the return value would be point to an
  77. undefined storage.
  78. -- 
  79. The Queen who loves, the Queen of life,    | smryan@netcom.com  PO Box 1563
  80. the Queen who straits, the Queen of strife;|          Cupertino, California
  81. with gasp of death or gift of breath       | (xxx)xxx-xxxx            95015
  82. she brings the choice of birth or knife.   |         I don't use no smileys
  83.